/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.zt.dangyuan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;
import io.zt.dangyuan.customView.SendEvent;
import io.zt.dangyuan.util.BaseRVAdapter;
import io.zt.dangyuan.util.BaseViewHolder;
import io.zt.dangyuan.util.Cons;


/**
 * TODO
 */
public class MainActivity extends BaseActivity implements View.OnClickListener,PopupWindow.OnDismissListener{
    private CircleMenuLayout mCircleMenuLayout;
    private String[] mItemTexts = new String[]{"心理解析", "支部活动", "志愿活动", "两学一做", "三会一课", "通知公告", "党建动态",
            "工作部署"};
    public static final String[] title_TQ = new String[]{"头条新闻", "党讯要闻", "青年之声"};
    public static final String[] title_QN = new String[]{"心理健康测评", "心理健康辅导"};
    public static final String[] title_HD = new String[]{"评选活动", "活动发布", "活动回顾"};

    private int[] mItemImgs = new int[]{R.drawable.index1,
            R.drawable.index2, R.drawable.index3,
            R.drawable.index5,R.drawable.index4, R.drawable.index6, R.drawable.index7,
            R.drawable.index8};
    private RelativeLayout title,center,index1,index2,index3,index4,index5,index6,index7,index8;
    private CircleImageView circleImageView;
    private PopupWindow pop;
    private View contentView;
    RecyclerView recyclerStoredata;
    //左边弹出框
    String[] titles = {"首页", "党建动态", "通知公告", "工作部署", "两学一做", "志愿活动", "三会一课", "支部活动", "我的管理", "心理解析"};
    ArrayList<String> titleLeft;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_content);
        initView1();
    }

    private void initView1() {
        titleLeft = new ArrayList<String>();
        for (int i = 0; i < titles.length; i++) {
            titleLeft.add(i,titles[i]);
        }
        title = (RelativeLayout)findViewById(R.id.title);
        circleImageView = (CircleImageView)findViewById(R.id.id_circle_menu_center_img);
        center = (RelativeLayout) findViewById(R.id.center);
        index1 = (RelativeLayout) findViewById(R.id.index1);
        index2 = (RelativeLayout) findViewById(R.id.index2);
        index3 = (RelativeLayout) findViewById(R.id.index3);
        index4 = (RelativeLayout) findViewById(R.id.index4);
        index5 = (RelativeLayout) findViewById(R.id.index5);
        index6 = (RelativeLayout) findViewById(R.id.index6);
        index7 = (RelativeLayout) findViewById(R.id.index7);
        index8 = (RelativeLayout) findViewById(R.id.index8);
        center.setOnClickListener(this);
        index1.setOnClickListener(this);
        index2.setOnClickListener(this);
        index3.setOnClickListener(this);
        index4.setOnClickListener(this);
        index5.setOnClickListener(this);
        index6.setOnClickListener(this);
        index7.setOnClickListener(this);
        index8.setOnClickListener(this);
        title.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
//        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        nm.cancelAll();
        loadHead();
    }

    private void loadHead() {
        GlobalDataApplication app = (GlobalDataApplication) getApplication();
        if (app.getUserModel() != null) {
            Glide.with(this).load(Cons.BASE_URL + app.getUserModel().getUserimg()).placeholder(R.drawable.ic_laucher).into(circleImageView);
        } /*else {
            mCircleMenuLayout.getCenterImageview().setImageResource(R.drawable.ic_laucher);
        }*/
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.index4://两学一做
                startActivity(new Intent(MainActivity.this, ReadLove.class).putExtra("pos", 3));
                break;
            case R.id.index6://通知公告
                startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", 5).putExtra("titles", MainActivity.title_TQ));
                break;
            case R.id.index8://党建动态
                startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", 7));
                break;
            case R.id.index7://工作部署
                startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", 6));
                break;
            case R.id.index1://青年心理一解一
                startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", 0).putExtra("titles", MainActivity.title_QN));
                break;
            case R.id.index2://支部活动
                startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", 1).putExtra("titles", MainActivity.title_HD));
                break;
            case R.id.index3://志愿活动
                startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", 2));
                break;
            case R.id.index5://三会一课
                startActivity(new Intent(MainActivity.this, ActivityLife.class));
                break;
            case R.id.center:
            //判断，未登录跳转登陆，登陆跳转管理
            final GlobalDataApplication app = (GlobalDataApplication) getApplication();
            if (app.getUserModel() != null) {
                startActivity(new Intent(MainActivity.this, MyAdminActivity.class));
            } else {
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
            }
            break;
            case R.id.title://左侧弹出框
                makepop();
                break;
        }
    }

    //弹出框pop
    private void makepop() {
        if (pop == null || contentView == null) {
            contentView = LayoutInflater.from(this).inflate(R.layout.recyclerview_list, null);
            pop = new PopupWindow(contentView);
            pop.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            pop.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            pop.setOutsideTouchable(true);
            pop.setFocusable(true);
            pop
                    .setBackgroundDrawable(this
                            .getResources()
                            .getDrawable(
                                    R.drawable.drawable_white));
            backgroundAlpha(0.5f);
            // 添加pop窗口关闭事件
            pop.setOnDismissListener(this);
        }
        backgroundAlpha(0.5f);
        pop.showAsDropDown(title, 0, 0, Gravity.NO_GRAVITY);
        recyclerStoredata = contentView.findViewById(R.id.recycler);
        recyclerStoredata.setLayoutManager(new GridLayoutManager(MainActivity.this, 1, LinearLayoutManager.VERTICAL, false));
        initList();
    }
    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = MainActivity.this.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        MainActivity.this.getWindow().setAttributes(lp);
    }


    /*
  *左边弹出框
  */
    private void initList() {
        BaseRVAdapter adapter = new BaseRVAdapter(MainActivity.this, titleLeft) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.text_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.text, titleLeft.get(position));
            }
        };

        adapter.addItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                int intentPos=0;
                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                        return;
                    case 8://判断，未登录跳转登陆，登陆跳转管理

                        final GlobalDataApplication app = (GlobalDataApplication)MainActivity.this.getApplication();

                        if (app.getUserModel()!=null)
                        {
                            startActivity(new Intent(MainActivity.this, MyAdminActivity.class));
                        }else
                        {
                            startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        }

//                        startActivity(new Intent(MainActivity.this, MyAdminActivity.class));
                        return;
                    case 1:intentPos=6; break;
                    case 2:intentPos=5; break;
                    case 3:intentPos=7; break;
                    case 4:intentPos=2; break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, ReadLove.class));
                        return;

                    case 6:intentPos=4; break;
                    case 7:intentPos=1; break;
                    case 9:intentPos=0; break;
                }
                switch (intentPos) {
                    case 5://团青时讯
                        startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", intentPos).putExtra("titles", MainActivity.title_TQ));
                        break;
                    case 6://党建动态
                        startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", intentPos));
                        break;
                    case 7://工作部署
                        startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", intentPos));
                        break;
                    case 0://青年心理一解一
                        startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", intentPos).putExtra("titles", MainActivity.title_QN));
                        break;
                    case 1://活动超市
                        startActivity(new Intent(MainActivity.this, ActivityItem2.class).putExtra("pos", intentPos).putExtra("titles", MainActivity.title_HD));
                        break;
                    case 2://众筹公益
                        startActivity(new Intent(MainActivity.this, ActivityItem1.class).putExtra("pos", intentPos));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, ActivityLife.class));
                        break;
                }

            }
        });
        EventBus.getDefault().post(new SendEvent(1));
        recyclerStoredata.setAdapter(adapter);
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1f);
    }
}
